function CollectionTabs(service,tabs) {
   this.service = service;
   this.contentTabs = tabs;
   this.parent = null;
   this.xsltCache = null;
}

CollectionTabs.prototype.init = function(doc,parent,xsltCache) {
   if (!this.contentTabs) {
      this.contentTabs = doc.getElementById('content-tabs');
   }
   this.parent = parent;
   this.xsltCache = xsltCache;
}

CollectionTabs.prototype.getCollectionTab = function(id) {
   var tabpanels = this.contentTabs.getElementsByTagName("tabpanels").item(0);
   var tabsContainer = this.contentTabs.getElementsByTagName("tabs").item(0);
   var tabs = tabsContainer.getElementsByTagName("tab");
   var list = tabpanels.getElementsByTagName("tabpanel");
   for (var i=0; i<list.length; i++) {
      if (list[i].getAttribute("id")==id) {
         return {
            index: i,
            panel: list[i],
            tab: tabs[i]
         }
      }
   }
   return null;
}

CollectionTabs.prototype.createCollectionTab = function(label,id) {
   var tabpanels = this.contentTabs.getElementsByTagName("tabpanels").item(0);
   var tabsContainer = this.contentTabs.getElementsByTagName("tabs").item(0);
   var tabs = tabsContainer.getElementsByTagName("tab");
   var tab = tabsContainer.appendItem(label,null);
   tab.setAttribute("onclick","Service.selectCollection('"+id+"')");

   var panel = tabpanels.ownerDocument.createElementNS(XUL_NS,"tabpanel");
   panel.setAttribute("flex","2");
   panel.setAttribute("id",id);
   panel.setAttribute("style","overflow: auto");
   tabpanels.appendChild(panel);
   return {
      index: tabs.length,
      panel: panel,
      tab: tab
   }
}

CollectionTabs.prototype.selectCollection = function(id) {
   var tabpanels = this.contentTabs.getElementsByTagName("tabpanels").item(0);
   var tabsContainer = this.contentTabs.getElementsByTagName("tabs").item(0);
   var tabs = tabsContainer.getElementsByTagName("tab");
   var list = tabpanels.getElementsByTagName("tabpanel");
   for (var i=0; i<list.length; i++) {
      if (list[i].getAttribute("id")==id) {
         tabs[i].selected = true;
         return;
      }
   }
}

CollectionTabs.prototype.showCollection = function(collection) {
   var tabinfo = this.getCollectionTab(collection.url);
   if (!tabinfo) {
      tabinfo = this.createCollectionTab(collection.title,collection.url);
   }
   if (!tabinfo.panel.firstChild || tabinfo.panel.firstChild.localName=="description") {
      DOMUtil.clearChildren(tabinfo.panel);
      var titleText = collection.feed.getTitle();
      this.parent.collection_data = { 
         title: titleText ? titleText.xml.textContent : "",
         timeout: _TIMEOUT_,
         collection: collection,
         xsltCache: this.xsltCache,
         plugins: this.plugins,
         logLevel: LOG.level
      };
      var vbox = tabinfo.panel.ownerDocument.createElementNS(XUL_NS,"vbox");
      vbox.setAttribute("flex","2");
      vbox.setAttribute("style","overflow: auto");
      tabinfo.panel.appendChild(vbox);
      var iframe = tabinfo.panel.ownerDocument.createElementNS(XUL_NS,"iframe");
      iframe.setAttribute("flex","2");
      iframe.setAttribute("src","collection.xul");
      vbox.appendChild(iframe);
      var current = this;
      setTimeout(
         function() {
            current.parent.collection_data = null;
         },
         600
      );
   }
   this.contentTabs.selectedTab = tabinfo.tab;
   this.contentTabs.selectedPanel = tabinfo.panel;

}

CollectionTabs.prototype.closeCurrentCollection = function() {
   if (!this.contentTabs.selectedTab) {
      return;
   }
   var currentTab = this.contentTabs.selectedTab;
   var currentPanel = this.contentTabs.selectedPanel;

   var tabsContainer = this.contentTabs.getElementsByTagName("tabs").item(0);
   var tabpanels = this.contentTabs.getElementsByTagName("tabpanels").item(0);

   var dir = this.contentTabs.selectedIndex==0 ? 1 : -1
   tabsContainer.advanceSelectedTab(dir,true);

   tabsContainer.removeChild(currentTab);
   tabpanels.removeChild(currentPanel);

}

CollectionTabs.prototype.showLoadingCollection = function(collection) {
   var tabinfo = this.getCollectionTab(collection.url);
   if (!tabinfo) {
      tabinfo = this.createCollectionTab(collection.title,collection.url);
   }
   DOMUtil.clearChildren(tabinfo.panel);
   var desc = tabinfo.panel.ownerDocument.createElementNS(XUL_NS,"description");
   tabinfo.panel.appendChild(desc);
   desc.appendChild(desc.ownerDocument.createTextNode("Loading collection, please wait..."));
   this.contentTabs.selectedTab = tabinfo.tab;
   this.contentTabs.selectedPanel = tabinfo.panel;
}

